package com.qire.other.recorder;

import android.content.Context;

import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;

import java.util.HashMap;

/**
 * 友盟平台统计器
 */
public class UMActionDataRecorder implements ComicActionRecorder<UMActionDataRecorder> {

    private static final String APP_KEY = "5955b53ac8957650820004a6";

    private final String actionType;
    private HashMap<String, String> eventParams;

    protected UMActionDataRecorder(String actionType) {
        this.actionType = actionType;
    }

    /**
     * 预初始化
     * @param context APP上下文
     * @param channelNo 渠道号
     */
    public static void preInit(Context context, String channelNo) {
        // 预初始化SDK，应对审核合规操作，这个方法不会读取用户数据和上传用户相关数据
        UMConfigure.preInit(context, APP_KEY, channelNo);
    }

    /**
     * 初始化
     * @param context APP上下文
     * @param channelNo 渠道号
     */
    public static void init(Context context, String channelNo) {
        // 初始化SDK
        UMConfigure.init(context, APP_KEY, channelNo, UMConfigure.DEVICE_TYPE_PHONE, null);
        // 选用AUTO页面采集模式
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);
    }

    @Override
    public UMActionDataRecorder putParam(String key, String value) {
        if(eventParams == null){
            eventParams = new HashMap<>();
        }
        eventParams.put(key, value);
        return this;
    }

    @Override
    public UMActionDataRecorder setComicInfo(int id, String name) {
        putParam("(漫画id)漫画名", "(" + id+ ")" + name);
        return this;
    }

    @Override
    public UMActionDataRecorder setSource(String source) {
        putParam("来源", source);
        return this;
    }

    @Override
    public void doRecorde(Context context) {
//        // 打开统计SDK调试模式
//        UMConfigure.setLogEnabled(true);
        if(eventParams == null) {
            MobclickAgent.onEvent(context, actionType);
        } else {
            MobclickAgent.onEvent(context, actionType, eventParams);
        }
    }

}
